home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 6 / QRZ Ham Radio Callsign Database - Volume 6.iso / pc / files / amiga / rhinosrc.lha / ttydriv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-23  |  2.8 KB  |  133 lines

  1. /* TTY input line editing
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. /*#include <conio.h>*/
  7. #include "global.h"
  8. #include "mbuf.h"
  9. #include "session.h"
  10. #include "tty.h"
  11. #include "socket.h"
  12.  
  13. #define OFF    0
  14. #define ON    1
  15.  
  16. #define LINESIZE    256
  17.  
  18. #define CTLU    21
  19. #define CTLR    18
  20. #define CTLW    23
  21. #define CTLZ    26
  22. #define DEL    0x7f
  23.  
  24. /* Accept characters from the incoming tty buffer and process them
  25.  * (if in cooked mode) or just pass them directly (if in raw mode).
  26.  *
  27.  * Echoing (if enabled) is direct to the raw terminal. This requires
  28.  * recording (if enabled) of locally typed info to be done by the session
  29.  * itself so that edited output instead of raw input is recorded.
  30.  */
  31. char *
  32. ttydriv(sp,c)
  33. struct session *sp;
  34. char c;
  35. {
  36.     char *cp,*rp;
  37.  
  38.     switch(sp->ttystate.edit){
  39.     case OFF:
  40.         rp = cp = mallocw(2);
  41.         *cp++ = c;
  42.         *cp = '\0';
  43.         if(sp->ttystate.echo) {
  44.             fputc(c,Current->output);
  45.             fflush(Current->output);
  46.         }
  47.         return rp;
  48.     case ON:
  49.         if(sp->ttystate.line == NULLCHAR)
  50.             sp->ttystate.lp = sp->ttystate.line = calloc(1,LINESIZE);
  51.  
  52.         cp = sp->ttystate.lp;
  53.         rp = sp->ttystate.line;
  54.         /* Perform cooked-mode line editing */
  55.         switch(c & 0x7f){
  56.         case '\r':      /* CR and LF both terminate the line */
  57.         case '\n':
  58.             if(sp->ttystate.crnl)
  59.                 *cp++ = '\n';
  60.             else
  61.                 *cp++ = c;
  62.             *cp++ = '\0';
  63.             if(sp->ttystate.echo)
  64.                 putc('\n',Current->output);
  65.  
  66.             sp->ttystate.line = NULLCHAR;
  67.             return rp;
  68.         case DEL:
  69.         case '\b':      /* Character delete */
  70.             if(sp->ttystate.lp != rp){
  71.                 sp->ttystate.lp--;
  72.                 if(sp->ttystate.echo)
  73.                     fputs("\b \b",Current->output);
  74.             }
  75.             break;
  76.         case CTLR:    /* print line buffer */
  77.             if(sp->ttystate.echo){
  78.                 fprintf(Current->output,"^R\n");
  79.                 fwrite(rp,1,cp-rp,Current->output);
  80.             }
  81.             break;
  82.         case CTLU:    /* Line kill */
  83.             while(sp->ttystate.lp != rp){
  84.                 sp->ttystate.lp--;
  85.                 if(sp->ttystate.echo){
  86.                     fputs("\b \b",Current->output);
  87.                 }
  88.             }
  89.             break;
  90.         case CTLW:    /* Word kill */
  91.             while(cp != rp && isspace(cp[-1])){
  92.                 cp--;
  93.                 if(sp->ttystate.echo){
  94.                     fputs("\b \b",Current->output);
  95.                 }
  96.             }
  97.             while(cp != rp && !isspace(cp[-1])){
  98.                 cp--;
  99.                 if(sp->ttystate.echo){
  100.                     fputs("\b \b",Current->output);
  101.                 }
  102.             }
  103.             sp->ttystate.lp = cp;
  104.             break;
  105.         default:    /* Ordinary character */
  106.             *cp++ = c;
  107.  
  108.             /* ^Z apparently hangs the terminal emulators under
  109.              * DoubleDos and Desqview. I REALLY HATE having to patch
  110.              * around other people's bugs like this!!!
  111.              */
  112.             if(sp->ttystate.echo &&
  113. #ifndef AMIGA
  114.              c != CTLZ &&
  115. #endif
  116.              cp - rp < LINESIZE-1){
  117.                 putc(c,Current->output);
  118.  
  119.             } else if(cp - rp >= LINESIZE-1){
  120.                 putc('\007',Current->output);   /* Beep */
  121.                 cp--;
  122.             }
  123.             sp->ttystate.lp = cp;
  124.             break;
  125.         }
  126.         if(sp->ttystate.echo) {
  127.             fflush(Current->output);
  128.         }
  129.         break;
  130.     }
  131.     return NULLCHAR;
  132. }
  133.